const {verifyWebToken} = require("@common/utils")
const {send} = require("@common/utils")

const  verifyWebAuth = async (ctx,next) => {
 try {
  let {authorization} = ctx.headers

  if(!authorization) return send(ctx,401,'-1','您不具备操作权限,请登录')
  let token = authorization.replace("Bearer ","")
  let user = verifyWebToken(token)
  ctx.user = user
  await next()
  
 } catch (err) {

  switch (err.message) {
    case 'TokenExpiredError: jwt expired':
      send(ctx,401,"-1",'令牌失效,请重新登录')
      break;
    case 'JsonWebTokenError: invalid signature':
      send(ctx,401,"-1",'无效签名,请重新登录')
      break;
    default:
      throw new Error(err)
      break;
  }
 }
}

module.exports = {
  verifyWebAuth
}